﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OutdoorLib.BaseEntity.Infrastructure;
using Grishko.DataTier;
using Grishko.SerilizeTools;
using OutdoorLib.BaseEntity.Contract;

public partial class Ctl_ctlContractSheduleSideList : System.Web.UI.UserControl
{
    const string FilterIDKey = "FilterIDKey";
    
    public Func <ContractEntity> ContractSource;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtDtStart.Text = DateTime.Now.ToShortDateString();
            txtDtFinish.Text = DateTime.Now.ToShortDateString();
        }
        
        MyInit();

        if (!IsPostBack)            
                DataBind();
        
    }
        

    private void MyInit()
    {
        //Редактор фильтра - источник
        ctlFilterEditor1.DataSource = GetFilter;
        ctlFilterEditor1.ImChange = FilterParamsChanged;

        //Сетка - источник
        ctlSideSheduleRows1.ImListOfFilter.DataSource = GetFilter;

        // Сетка - параметры визуализации
        ctlSideSheduleRows1.SheduleParams = SideSheduleParams;

    }

    #region Источник данных
    Filter _FilterValue = null;
    public  Filter GetFilter()
    {
        if (null != _FilterValue)
                return _FilterValue;

        if (CacheTools.IsSessionCacheObject(FilterID))
            _FilterValue = (Filter)CacheTools.GetSessionCacheObect(FilterID);

        else
        {
            _FilterValue = (Filter)EntityActivator.CreateInstance__(FilterTagName);
            CacheTools.SetSessionCacheObject(FilterID, _FilterValue);
        }

        return _FilterValue ;

    }
    private string FilterID 
    { 
            get {
                if(null==ViewState [FilterIDKey])
                ViewState [FilterIDKey]=Guid.NewGuid ().ToString ();        
                return ViewState [FilterIDKey].ToString ();} 
    }
    private string FilterTagName { get { return "ConstractionSheduleFilter"; } }


    private Grishko.UI.SideSheduleTable.SideSheduleParams SideSheduleParams
    {
        get 
        {
            Grishko.UI.SideSheduleTable.SideSheduleParams result = new Grishko.UI.SideSheduleTable.SideSheduleParams();
            result.dtStart = txtDtStart.TexAsDateValue;
            result.dtFinish = txtDtFinish.TexAsDateValue;
            result.Period = CurrentTimePeriod;
            result.CurrentContract  = CurrentContract;
            return result;
        }
    }

    protected ContractEntity CurrentContract
    {
        get
        {
            if (null == ContractSource)
                return null;
            return ContractSource();
        }

    }

    protected Ecopsy.Tools.DateTimeTools.TimePeriod CurrentTimePeriod
    {
        get { return Ecopsy.Tools.DateTimeTools.ConvertToTimePeriod(cmbPeriodType.SelectedValue); }
    }

    #endregion

    
  
    protected bool FilterParamsChanged()
    {
        MyInit();
        DataBind();
        updRows.Update ();
        return true;
    }

    protected void btnFind_Click(object sender, ImageClickEventArgs e)
    {
        FilterParamsChanged();
    }
}
